home *** CD-ROM | disk | FTP | other *** search
- /*******************************************************************************
- +
- + LEDA 2.1.1 11-15-1991
- +
- +
- + ch_hashing.h
- +
- +
- + Copyright (c) 1991 by Max-Planck-Institut fuer Informatik
- + Im Stadtwald, 6600 Saarbruecken, FRG
- + All rights reserved.
- +
- *******************************************************************************/
-
-
-
-
-
- //------------------------------------------------------------------------------
- // Hashing with Chaining
- //
- // Walter Zimmer (1989)
- //------------------------------------------------------------------------------
-
- #ifndef CHHASHINGH
- #define CHHASHINGH
-
- #include <LEDA/basic.h>
- #include <LEDA/list.h>
-
- #define NOTEST
-
- #ifdef TEST
- #define TRACE(x) cout << form x
- #define TRACE2(x,ebene) if ( debug >= (ebene) ) cout << form x
- #define TRACE_FUNC(x) x
- // extern int debug ; muss im testprogramm definiert werden , falls
- // Trace2(x,ebene) benutzt wird
- #else
- #define TRACE(x)
- #define TRACE2(x,ebene)
- #define TRACE_FUNC(x)
- #endif
-
-
- //--------------------------------------------------------------------
- // some declarations and type definitions
- //--------------------------------------------------------------------
-
-
- class ch_hashing_el {
- ent k;
- ent e;
- friend class ch_hashing;
-
- public:
-
- ch_hashing_el(ch_hashing_el* p)
- {
- k = p->k;
- e = p->e;
- }
-
- ch_hashing_el(ent K = 0, ent E = 0)
- {
- k = K;
- e = E;
- }
-
- OPERATOR_NEW(2)
- OPERATOR_DEL(2)
-
- };
-
- typedef ch_hashing_el* list2_el;
- declare(list,list2_el);
- typedef list(list2_el) hash_list;
- typedef hash_list* hash_list_ptr;
-
- typedef int (*hash_fct) (ent&);
-
- extern int default_hash_fct(ent&);
-
- const int default_hash_tablesize = 1000;
-
- //--------------------------------------------------------------------
- // class ch_hashing
- //--------------------------------------------------------------------
-
- class ch_hashing {
-
- hash_list_ptr* hash_table;
- int table_size;
- int divisor; // divisor = 2^n-1 = 00..0011.11
- // => statt " modulo table_size " einfach " & divisor"
- hash_fct f;
- hash_list_ptr* iterator;
- int counter;
-
- int round(int) const;
- hash_list_ptr* get_list(ent) const;
- list_item get_list_el(ent,hash_list*) const;
- void new_size_or_fct(int, hash_fct);
-
- virtual int cmp(ent& x, ent& y) const { return int(x) - int(y); }
- virtual void clear_key(ent& x) const { x=0; }
- virtual void clear_inf(ent& x) const { x=0; }
- virtual void copy_key(ent& x) const { x=x; }
- virtual void copy_inf(ent& x) const { x=x; }
- virtual void print_key(ent& x) const { cout << int(x); }
-
- public:
-
- ch_hashing_el* lookup(ent) const;
- ch_hashing_el* insert(ent,ent);
- ch_hashing_el* del(ent);
-
- bool member(ent x) const { return ( lookup(x) ? true : false ); }
-
- ent& access(ent);
-
- ent key(ch_hashing_el* p) const { return p->k; }
- ent inf(ch_hashing_el* p) const { return p->e; }
- ent& info(ch_hashing_el* p) { return p->e; }
-
- bool change_obj(ent x, ent y);
- void change_inf( ch_hashing_el* p , ent y) { copy_inf(p->e = y) ; }
-
- bool empty() const { return counter ? false : true ; }
- int size() const { return counter; }
-
- int tablesize() const { return table_size ; }
-
- void print() const ;
-
- float average_list_length() const { return float(counter) / table_size ; }
-
- void init_iterator();
-
- ch_hashing_el* move_iterator();
-
- void new_table_size(int Tablesize) { new_size_or_fct(Tablesize,f); }
-
- void new_hash_fct(hash_fct F) { new_size_or_fct(table_size,F); }
-
- void new_size_and_fct(int Tablesize ,hash_fct F)
- { new_size_or_fct(Tablesize,F); }
-
- void clear();
- void remove();
-
- ch_hashing();
- ch_hashing(int);
- ch_hashing(hash_fct);
- ch_hashing(int,hash_fct);
-
- ~ch_hashing() { remove(); } // neben clear auch remove implem.
-
- }; // end of class ch_hashing
-
-
-
- #define forall_in_ch_hashing(k,t)\
- for( (t).init_iterator() ; (k)=(t).move_iterator() ; )
-
-
-
- #endif CHHASHINGH
-